def add():
    s = 0
    for i in range(1, 101):
        s += i
    print(s)


add()


# 递归
def add2(n):
    if n == 1:
        return 1
    return n + add2(n - 1)


print(add2(500))


def pbl(n):
    if n <= 1:
        return n
    return pbl(n - 2) + pbl(n - 1)


print(pbl(8))


# 闭包
def outer():  # 外层函数
    n = 10

    def inner():  # 内层函数
        print(n)  # 内层函数使用外层函数变量

    return inner


print(outer())  # 返回内层函数内存地址
# 调用 方式1
outer()()
# 方式2
ot = outer()
ot()


def outer(m):
    n = 10

    def inner(o):
        print("计算结果：", m + o)

    return inner


ot = outer(20)  # 调用外函数
ot(40)  # 调用内函数

# 函数引用
print(ot)
print(id(ot))


def outer(m):
    print("outer中m的值：", m)

    def inner(n):
        print("inner中n的值：", n)
        return m + n

    return inner


ot = outer(10)
print(ot)
print(ot(20))
print(ot(40))
print(ot(80))


# 使用闭包过程中，一旦外函数被调用一次，
# 返回了内函数的引用，虽然每次调用内函数会开启一个函数，执行后消亡

# 装饰器
# 语法1
def send():
    print("发送消息")


def outer(fn):
    def inner():
        print("登录...")
        fn()

    return inner


print(outer(send))
ot = outer(send)
ot()


# 语法2
def outer(fn):
    def inner():
        print("登录...")
        fn()

    return inner


@outer
def send():
    print("发送了消息：：：")


send()


@outer
def send2():
    print("验证码验证")


send2()


def outer(fn):
    def inner(name):
        print(f"{name} 是inner中参数")
        fn(name)

    return inner


@outer
def func(name):
    print("被装饰函数")


func("zhansan")


def outer(fn):
    def inner(*args, **kwargs):
        print("登录...")
        fn(*args, **kwargs)

    return inner


@outer
def fund(*args, **kwargs):
    print(args)
    print(kwargs)


# print(outer(fund))
# ot = outer(fund)
# ot(name='zhangsan', age=18)
fund()
fund(name="zhansan", age=18)


# 多个装饰器

def deco1(fn):
    def inner():
        return "deco1 inner开始" + fn() + "deco1结束"

    return inner


def deco2(fn):
    def inner():
        return "deco2 inner开始" + fn() + " deco2结束"

    return inner


# 多个装饰器，离最近的先装饰
@deco1
@deco2
def test1():
    return "晚上学习python"


print(test1())
